

/**
* @file CarrierRange.cpp
* Class to convert observables, from 3-char to 2-char.
*
* author: kaifa kuang
* time  : 2019/01/10
*/

#include "CarrierRange.hpp"

using namespace std;

namespace gpstk
{

      // Return a string identifying this object.
    string CarrierRange::getClassName() const
    { return "CarrierRange"; }


      /* Return a satTypeValueMap object, adding the new data generated
       *  when calling this object.
       *
       * @param time      Epoch corresponding to the data.
       * @param gData     Data object holding the data.
       */
    satTypeValueMap& CarrierRange::Process( const CommonTime& time,
                                                  satTypeValueMap& gData )
        noexcept(false)
    {
        try
        {
            SatIDSet satRejectedSet;

            // Loop through all the satellites
            for(satTypeValueMap::iterator it = gData.begin();
                it != gData.end();
                ++it)
            {
                SatID sat( it->first );

                typeValueMap::iterator itType;

                if(sat.system == SatelliteSystem::GPS)
                {
                    // C1
                    itType = it->second.find(TypeID::C1W);
                    if( itType != it->second.end() )
                    {
                        it->second[TypeID::C1] = it->second[TypeID::C1W];
                        it->second.removeTypeID(TypeID::C1W);
                    }

                    // L1
                    if( it->second.find(TypeID::L1C) != it->second.end() )
                    {
                        it->second[TypeID::L1] = it->second[TypeID::L1C];
                    }
                    else if( it->second.find(TypeID::L1W) != it->second.end() )
                    {
                        it->second[TypeID::L1] = it->second[TypeID::L1W];
                    }


                    // C2
                    itType = it->second.find(TypeID::C2W);
                    if( itType != it->second.end() )
                    {
                        it->second[TypeID::C2] = it->second[TypeID::C2W];
                    }

                    // L2
                    if( it->second.find(TypeID::L2W) != it->second.end() )
                    {
                        it->second[TypeID::L2] = it->second[TypeID::L2W];
                    }
                    else if( it->second.find(TypeID::L2X) != it->second.end() )
                    {
                        it->second[TypeID::L2] = it->second[TypeID::L2X];
                    }

                    // C5
                    itType = it->second.find(TypeID::C5X);
                    if( itType != it->second.end() )
                    {
                        it->second[TypeID::C5] = it->second[TypeID::C5X];
                        it->second.removeTypeID(TypeID::C5X);
                    }

                    // L5
                    itType = it->second.find(TypeID::L5X);
                    if( itType != it->second.end() )
                    {
                        it->second[TypeID::L5] = it->second[TypeID::L5X];
                        it->second.removeTypeID(TypeID::L5X);
                    }

                }
                else if(sat.system == SatelliteSystem::Galileo)
                {
                    // C1
                    itType = it->second.find(TypeID::C1X);
                    if( itType != it->second.end() )
                    {
                        it->second[TypeID::C1] = it->second[TypeID::C1X];
                        it->second.removeTypeID(TypeID::C1X);
                    }

                    // C5
                    itType = it->second.find(TypeID::C5X);
                    if( itType != it->second.end() )
                    {
                        it->second[TypeID::C5] = it->second[TypeID::C5X];
                        it->second.removeTypeID(TypeID::C5X);
                    }

                    // C7
                    itType = it->second.find(TypeID::C7X);
                    if( itType != it->second.end() )
                    {
                        it->second[TypeID::C7] = it->second[TypeID::C7X];
                        it->second.removeTypeID(TypeID::C7X);
                    }
                    // C8
                    itType = it->second.find(TypeID::C8X);
                    if( itType != it->second.end() )
                    {
                        it->second[TypeID::C8] = it->second[TypeID::C8X];
                        it->second.removeTypeID(TypeID::C8X);
                    }
                    // C6
                    itType = it->second.find(TypeID::C6X);
                    if( itType != it->second.end() )
                    {
                        it->second[TypeID::C6] = it->second[TypeID::C6X];
                        it->second.removeTypeID(TypeID::C6X);
                    }

                    // L1
                    itType = it->second.find(TypeID::L1X);
                    if( itType != it->second.end() )
                    {
                        it->second[TypeID::L1] = it->second[TypeID::L1X];
                        it->second.removeTypeID(TypeID::L1X);
                    }

                    // L5
                    itType = it->second.find(TypeID::L5X);
                    if( itType != it->second.end() )
                    {
                        it->second[TypeID::L5] = it->second[TypeID::L5X];
                        it->second.removeTypeID(TypeID::L5X);
                    }

                    // L7
                    itType = it->second.find(TypeID::L7X);
                    if( itType != it->second.end() )
                    {
                        it->second[TypeID::L7] = it->second[TypeID::L7X];
                        it->second.removeTypeID(TypeID::L7X);
                    }

                    // L8
                    itType = it->second.find(TypeID::L8X);
                    if( itType != it->second.end() )
                    {
                        it->second[TypeID::L8] = it->second[TypeID::L8X];
                        it->second.removeTypeID(TypeID::L8X);
                    }
                    // L6
                    itType = it->second.find(TypeID::L6X);
                    if( itType != it->second.end() )
                    {
                        it->second[TypeID::L6] = it->second[TypeID::L6X];
                        it->second.removeTypeID(TypeID::L6X);
                    }


                }
                else if(sat.system == SatelliteSystem::BDS)
                {
                    // C2
                    itType = it->second.find(TypeID::C2I);
                    if( itType != it->second.end() )
                    {
                        it->second[TypeID::C2] = it->second[TypeID::C2I];
                        it->second.removeTypeID(TypeID::C2I);
                    }

                    // L2
                    itType = it->second.find(TypeID::L2I);
                    if( itType != it->second.end() )
                    {
                        it->second[TypeID::L2] = it->second[TypeID::L2I];
                        it->second.removeTypeID(TypeID::L2I);
                    }

                    // C7
                    itType = it->second.find(TypeID::C7I);
                    if( itType != it->second.end() )
                    {
                        it->second[TypeID::C7] = it->second[TypeID::C7I];
                        it->second.removeTypeID(TypeID::C7I);
                    }

                    // L7
                    itType = it->second.find(TypeID::L7I);
                    if( itType != it->second.end() )
                    {
                        it->second[TypeID::L7] = it->second[TypeID::L7I];
                        it->second.removeTypeID(TypeID::L7I);
                    }

                    // C6
                    itType = it->second.find(TypeID::C6I);
                    if( itType != it->second.end() )
                    {
                        it->second[TypeID::C6] = it->second[TypeID::C6I];
                        it->second.removeTypeID(TypeID::C6I);
                    }

                    // L6
                    itType = it->second.find(TypeID::L6I);
                    if( itType != it->second.end() )
                    {
                        it->second[TypeID::L6] = it->second[TypeID::L6I];
                        it->second.removeTypeID(TypeID::L6I);
                    }

                }
                else if(sat.system == SatelliteSystem::GLONASS)
                {
                    satRejectedSet.insert(sat);
                }

            }  // End of 'for (it = gData.begin(); it != gData.end(); ++it)'

            gData.removeSatID(satRejectedSet);

            return gData;
        }
        catch(Exception& u)
        {
            // Throw an exception if something unexpected happens
            ProcessingException e( getClassName() + ":" + u.what() );
            GPSTK_THROW(e);
        }

    }  // End of method 'CarrierRange::Process()'


     /** Return a sourceRinex object, adding the new data generated when
      *  calling a modeling object.
      *
      * @param gData    Data object holding the data.
      */
    void CarrierRange::Process(commonHeader& comHeader,sourceRinex& gData)
        noexcept(false)
    {
        for(sourceDataMap::iterator sdmIt = gData.body.begin();
            sdmIt != gData.body.end();
            ++sdmIt)
        {
            Process( gData.header.epoch, sdmIt->second );
        }

    }  // End of method 'CarrierRange::Process()'


}  // End of namespace gpstk
